package cn.liumouren.jzxd.service.impl;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.Query;
import cn.liumouren.jzxd.dao.CommentDao;
import cn.liumouren.jzxd.dao.UserDao;
import cn.liumouren.jzxd.entity.Comment;
import cn.liumouren.jzxd.service.CommentService;
import cn.liumouren.jzxd.utils.mapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Map;


@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {

    @Resource
    private UserDao userDao;
    @Resource
    private CommentDao commentDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Comment> page = this.page(
                new Query<Comment>().getPage(params),
                new QueryWrapper<Comment>()
        );

        return new PageUtils(page);
    }

    @Override
    public IPage<Comment> getPageCommentByZoneId(String zoneId, int cur, int size) {
        IPage<Comment> page = new Page<>(cur, size);

        /*IPage<Comment> commentPage = baseMapper.selectPage(page, new QueryWrapper<Comment>().lambda()
                .eq(Comment::getZoneId, zoneId)
                .orderByDesc(Comment::getAddTime));

        // 获取 user 和 replyUser
        List<Comment> comments = commentPage.getRecords();
        mapperUtils.getUserAndReplyUserByComments(comments, userDao);
        commentPage.setRecords(comments);*/

        IPage<Comment> commentPage = commentDao.getPageCommentByZoneId(page, zoneId);

        return commentPage;
    }

    @Override
    public int getCommentCount(String zoneId) {
        Integer count = baseMapper.selectCount(new LambdaQueryWrapper<Comment>().eq(Comment::getZoneId, zoneId));
        return count == null ? 0 : count;
    }

    @Override
    public IPage<Comment> getMainComment(String zoneId, int cur, int size) {
        // 查询所有 replyUserId 为 null 的评论, 即为主评论
        IPage<Comment> comments = baseMapper.selectPage(new Page<Comment>(cur, size, false),
                new LambdaQueryWrapper<Comment>()
                        .isNull(Comment::getReplyUserId)
                        .eq(Comment::getZoneId, zoneId)
                        // 时间降序
                        .orderByDesc(Comment::getAddTime));
        return mapperUtils.getUserAndReplyUserByComments(comments, userDao);
    }

    @Override
    public IPage<Comment> getCommentedComment(String zoneId, String replyUserId, int cur, int size) {
        IPage<Comment> comments = baseMapper.selectPage(new Page<Comment>(cur, size, false),
                new LambdaQueryWrapper<Comment>()
                        .eq(Comment::getReplyUserId, replyUserId)
                        // 时间降序
                        .orderByDesc(Comment::getAddTime));
        return mapperUtils.getUserAndReplyUserByComments(comments, userDao);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Comment entity) {
        entity.setId(IdWorker.getIdStr());
        entity.setAddTime(new Date());
        return SqlHelper.retBool(baseMapper.insert(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(Comment entity) {
        return SqlHelper.retBool(baseMapper.updateById(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return false;
        }
        return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
    }

}